<%-- 
    Document   : registration
    Created on : Jan 15, 2010, 11:14:18 PM
    Author     : 50920709
--%>

<%@page contentType="text/html;charset=UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>Web Baesd Intelligent Tutoring System in Sudoku for Logic Training</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <%-- CSS is get from http://www.freelayouts.com/ --%>
    <link rel="stylesheet" type="text/css" href="css/default.css" title="default"/>
</head>

<script type="text/javascript">
function validateRegForm(regform){

    var alertReason="";
    if(isCompleteForm(regform)){
        /*valid the userInput*/
        if (!checkEmail(regform.email.value)){
            alertReason += "Email Address is Invalid!\n";
             alertReason += "電子郵件地址無效!\n";
        }
        if(!checkPwd(regform.password.value)){
            alertReason += "Password is Invalid!\n"
            alertReason += "密碼是無效的!\n"
        }
        if(!checkInteger(regform.age.value)){
            alertReason += "Age is Invalid!\n";
            alertReason += "年齡是無效的!\n";

        }
        if(!checkInteger(regform.experience.value)){
            alertReason += "Sudoku Experience is Invalid\n";
            alertReason += "數獨經驗是無效的!\n";
        }
        if(alertReason != ""){
            alert(alertReason);
            return false;
        }
        return true;
    }
    return false;
}

function isCompleteForm(regform){

    /* Check for any input is empty */
    var alertReason = "";
	if ((regform.email.value==null)||(regform.email.value=="")){
		alertReason += "Please Enter your Email!\n";
        alertReason += "請輸入您的電子郵件!\n";
	}
    if ((regform.email.value==null)||(regform.password.value=="")){
		alertReason += "Please Enter your Password!\n";
        alertReason += "請輸入您的密碼!\n";
	}
    if (!isRadioChecked(regform.sex)){
		alertReason += "Please Select your Sex!\n";
        alertReason += "請選擇您的性別!\n";
	}
    if ((regform.age.value==null)||(regform.age.value=="")){
		alertReason += "Please Enter your Age!\n";
        alertReason += "請輸入您的年齡!\n";
	}
    if ((regform.eduLevel.value==null)||(regform.eduLevel.value=="")){
		alertReason += "Please Enter your Education Level!\n";
        alertReason += "請輸入您的教育水平!\n";
	}
    if ((regform.experience.value==null)||(regform.experience.value=="")){
		alertReason += "Please Enter your Sudoku Experience!\n";
        alertReason += "請輸入您的數獨經驗!\n";
	}
    if (!isRadioChecked(regform.langMode)){
		alertReason += "Please Enter your Language Mode!\n";
        alertReason += "請輸入您的語言模式!\n";
	}

    if(alertReason != ""){
       alert(alertReason);
       return false;
    }

    return true;

}
/* copy from http://javascript.internet.com/forms/email-validation---basic.html */
function checkEmail(email) {

		var at="@";
		var dot=".";
		var lat=email.indexOf(at);
		var lstr=email.length;
		var ldot=email.indexOf(dot);
		if (email.indexOf(at)==-1){
		   return false;
		}
		if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){
		   return false;
        }
		if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){
		    return false;
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
         }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }

		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true;
}
function checkPwd(password){
    if(password.length >=6  && password.length <= 20){
        return true;
    }
    return false;
}
function checkInteger(checkStr){

    var intRegExp="^[0-9]*$";
    if(checkStr.match(intRegExp)){
        return true;
    }
    return false;
}

function isRadioChecked(radio){

    for(i=0; i<radio.length; i++){
        if(radio[i].checked){
            return true;
        }
    }
    return false;
}
</script>

<body>
    <jsp:include page='header.jsp'/>
    <%@ include file='DatabaseSetup.jsp'%>

        <div class="article_body">
            <form name="RegForm" method="post" onsubmit="return validateRegForm(this)">
                <table name="RegTable" class="regtable">
                    <tr>
                        <td>Email Address 電子郵件地址</td>
                        <td><input type="text" name="email" ></td>
                        <td class="given"></td>
                    </tr>
                    <tr>
                        <td>Password 密碼</td>
                        <td><input type="password" name="password"></td>
                        <td class="given">
                            Password Length from 6 to 20<br/>
                            密碼長度6〜20
                        </td>
                    </tr>
                    <tr>
                        <td>Sex 性別</td>
                        <td>
                             <input type="radio" class="radio" name="sex" value="1" /> Male 男性 <br/>
                             <input type="radio" class="radio" name="sex" value="0" /> Female 女性 <br/>
                             <td class="given"></td>
                        </td>
                    </tr>
                    <tr>
                        <td>Age 年齡</td>
                        <td><input type="text" name="age"/></td>
                        <td class="given"></td>
                    </tr>
                    <tr>
                        <td>Education Level 教育水平 </td>
                        <td>
                            <select name="eduLevel">
                                <option value="kindergarden">Kindergarden 幼兒園 </option>
                                <option value="primary">Primary school 小學</option>
                                <option value="secondary">Secondary School 中學</option>
                                <option value="asso">Associate degree 副學士</option>
                                <option value="degree">Degree 學士</option>
                                <option value="master">Master 碩士</option>
                                <option value="doctor">Doctor 博士</option>
                                <option value="other">Others 其他</option>
                            </select>
                         </td>
                         <td class="given"></td>
                    </tr>
                    <tr>
                        <td>Sudoku Experience 數獨經驗</td>
                        <td><input type="text" name="experience"/></td>
                        <td class="given">
                            In Month<br/>
                            以月計
                        </td>
                    </tr>
                    <tr>
                        <td>Language Mode 語言模式 </td>
                        <td>
                             <input type="radio" class="radio" name="langMode" value="1" /> English 英文 <br/>
                             <input type="radio" class="radio" name="langMode" value="0" /> Chinese 中文 <br/>
                        </td>
                        <td class="given"></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input type="submit" class="submit" name="submit" value="Submit"></td>
                        <td class="given"></td>
                    </tr>

                </table>
            </form>
       
            <p>
     <%
        String email=request.getParameter("email");
        String password=request.getParameter("password");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String eduLevel = request.getParameter("eduLevel");
        String experience = request.getParameter("experience");
        String langMode = request.getParameter("langMode");

        java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
        java.util.Date date = new java.util.Date();
        String dateTime = dateFormat.format(date);

        boolean isInserted = false;
        try{

            String sql = "select Count(*) from userInfo where Email = " + "\""+email+"\"";
            ResultSet rs = stmt.executeQuery(sql);
            rs.next();
   
            if(Integer.parseInt(rs.getString(1))!=0){
                out.println("<h2>Registration Failed,Please register using another email address.<br/>");
                out.println("註冊失敗，請使用其他的電子郵件地址.<br/></h2>");
            }else{
                sql = "INSERT INTO userinfo(`Email`, `Password`, `Sex`, " +
                        "`Age`, `EducationLevel`, `SudokuExperience`, `LangMode`, `Timestamp`)" +
                        "VALUES (" +
                        "'"+email+"', " +
                        "'"+password+"', " +
                        "'"+sex+"', " +
                        "'"+age+"', " +
                        "'"+eduLevel+"', " +
                        "'"+experience+"', " +
                        "'"+langMode+"', " +
                        "'"+dateTime+"' ) ";
                isInserted =(stmt.executeUpdate(sql)> 0);

                sql = "select userID from userInfo where Email = " + "\""+email+"\"";
                rs = stmt.executeQuery(sql);
                rs.next();
                sql = "INSERT INTO gameautohintsgivingtime VALUES(" +
                        "'"+rs.getString(1)+"', " +
                        "'"+15+"', " +
                        "'"+10+"', " +
                        "'"+5+"' " +
                        ")";

                isInserted = (isInserted && (stmt.executeUpdate(sql)> 0));

                if(isInserted){
                    String loginFormURL ="http://144.214.121.25:8080/SudokuITSWebsite/login.jsp";
                    response.setHeader("Refresh", "3; URL=" + loginFormURL);
                    out.println("<h2>Registration Success! You will be redirected to login page after 3 second. If not, press <a href=\"login.jsp\">here</a><br/>");
                    out.println("己注册!3秒後進入登入頁面，如沒有，請按<a href=\"login.jsp\">此</a><br/></h2>");
                }
                /*else{
                    out.println("<h2>Registration Failed,Please contact system admin.<br/>");
                    out.println("註冊失敗，請與系統管理員聯絡.<br/></h2>");
                }*/
           }
            
           rs.close();
           con.close();
           stmt.close();

        }catch(Exception e){}
    %>
            </p>
     </div>
    <jsp:include page='footer.jsp'/>
</body>
</html>

